/**
 * Copyright (c) 2016-2100 yunclouds
 */
package com.yunqian.web;

import com.yunqian.common.CurrentUser;
import com.yunqian.jwt.JwtUser;
import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.domain.Banner;
import com.yunqian.redot.service.BannerService;
import com.yunqian.redot.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Describe: 轮播图管理信息控制器
 *
 * @author zsy
 * 2018-09-29 16:01:12
 */
@RestController
@RequestMapping("/api/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Banner> pagination(@RequestBody Map<String, Object> params) {
        return bannerService.pagination(params);
    }

    /**
     * 获取所有列表
     *
     * @return
     */
    @GetMapping("/all")
    public Map<String, Object> list() {
        List<Banner> bannerList = bannerService.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("bannerList", bannerList);
        return map;
    }

    /**
     * 添加
     *
     * @param banner 对象信息
     */
    @PostMapping
    public Banner save(@RequestBody @Valid Banner banner, @CurrentUser JwtUser user) {
        bannerService.save(banner);
        operationLogService.save(OperationMenus.BANNER_LIST, Operation.ADD, user.getId());
        return banner;
    }

    /**
     * 局部更新数据
     *
     * @param id         待更新id
     * @param modifyData 待更新的数据
     * @return
     */
    @PutMapping("/{id}")
    public Banner update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData, @CurrentUser JwtUser user) {
        Banner banner = bannerService.update(id, modifyData);
        operationLogService.save(OperationMenus.BANNER_LIST, Operation.UPDATE, user.getId());
        return banner;
    }

    /**
     * 根据id获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public Banner detail(@PathVariable("id") Long id) {
        return bannerService.findStrictOne(id);
    }

    /**
     * 删除数据
     *
     * @param ids 多个id使用逗号分隔
     */
    @DeleteMapping("/{ids}")
    public void delete(@PathVariable("ids") Long[] ids, @CurrentUser JwtUser user) {
        for (Long id : ids) {
            bannerService.delete(id);
            operationLogService.save(OperationMenus.BANNER_LIST, Operation.REMOVE, user.getId());
        }
    }

}
